package entityClasses;



import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;







import com.google.appengine.api.datastore.EmbeddedEntity;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Key;



public class Game implements Serializable{

     /**
	 * It's Serializable because we need to save it to session
	 */
	private static final long serialVersionUID = 1L;
	
		private ArrayList<Question> questions;




       public Game(ArrayList<Question> questions) {
			super();
			this.questions = questions;
		}




		public Game(Entity game) {
		super();
		//Receive the list of embedded entity.
		 @SuppressWarnings("unchecked")
		ArrayList<EmbeddedEntity> questionsArrEntity = (ArrayList<EmbeddedEntity>) game.getProperty("questionsArrEntity");
		
		 //Create new questions list.
		 this.questions = new ArrayList<Question>();
		 
		 //Create question from each embedded entity.
		 for (EmbeddedEntity embeddedEntity : questionsArrEntity){
			 
			 //From embedded entity to entity.
			 Key key = embeddedEntity.getKey();
			 
			 //Entity with key from embeddedEntity.
			 Entity entityQuestion = new Entity(key);
			 
			//Import data to entity.
			 entityQuestion.setPropertiesFrom(embeddedEntity);
			 
			 //Create question from entity and push to List.
			 this.questions.add(new Question(entityQuestion));
		 }//End for
		 
		
		}//End constructor.




		public ArrayList<Question> getQuestions() {
			return questions;
		}




		public void setQuestions(ArrayList<Question> questions) {
			this.questions = questions;
		}




		//Create entity from this question.
        public Entity toEntity(String name) {
        			
        		//Create game entity.
                Entity entityGame = new Entity("game",name);
               
                //List of embedded entities for the questions
                List<EmbeddedEntity> questionsArrEntity = new ArrayList<EmbeddedEntity>();
                
                /*Convert every question to entity,
                 * than to embedded entity,
                 * and insert to list of embedded entities.
                 */
                for (Question question: this.questions){
                	
                	//Receive question as entity.
                	Entity eQuestion = question.toEntity();
                	
                	//Receive key
                	Key key = eQuestion.getKey();
                	
                    //Embedded, in order to insert it to the game.
                    EmbeddedEntity embeddedEntityQuestion = new EmbeddedEntity();
                	
                    //Setting the entity key to embedded entity
                    embeddedEntityQuestion.setKey(key);
                    
                    //Receive the properties of the question entity.
                    embeddedEntityQuestion.setPropertiesFrom(eQuestion);
                    
                    //Add to array.
                	questionsArrEntity.add(embeddedEntityQuestion);
                	
                }//End for

                //Insert the embedded questionsArrEntity to the game.
                entityGame.setProperty("questionsArrEntity", questionsArrEntity);
                
                return entityGame;
        }//End to entity.
        


}//End class.


